package com.ly.hotel.view.setting;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ly.hotel.R;
import com.ly.hotel.common.Constants;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.model.Device;
import com.ly.hotel.util.HardInfoUtil;

public class SysinfoLayout extends LinearLayout {

	private TextView roomNoTxt;
	private TextView baudTxt;
	private TextView ipTxt;
	private TextView networkMaskTxt;
	private TextView gatewayTxt;
	private TextView dnsTxt;
	private TextView macTxt;
	private TextView versionTxt;
	private TextView versionDataTxt;	
	private Device deviceInfo;
	private Context context;
	private MyApplication app;
	 
	public SysinfoLayout(Context c, Device d) {
		super(c);
		this.context = c; 
		this.app = (MyApplication)context.getApplicationContext();
		this.deviceInfo = d;
		LayoutInflater.from(getContext()).inflate(R.layout.layout_sysinfo, this);
		initView();

	}

	private void initView() {
		
		PackageManager pm = context.getPackageManager();
		int flags = PackageManager.GET_UNINSTALLED_PACKAGES;
		PackageInfo pkgInfo = null;
		try {
			pkgInfo = pm.getPackageInfo("com.ly.hotel", flags);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		
		roomNoTxt = (TextView)  findViewById(R.id.roomNoTxt);
		baudTxt = (TextView)  findViewById(R.id.baudTxt);
		ipTxt = (TextView) findViewById(R.id.ipTxt);
		networkMaskTxt = (TextView) findViewById(R.id.networkMaskTxt);
		gatewayTxt = (TextView)  findViewById(R.id.gatewayTxt);
		dnsTxt = (TextView)  findViewById(R.id.dnsTxt);
		macTxt = (TextView) findViewById(R.id.macTxt);
		versionTxt = (TextView) findViewById(R.id.versionTxt);
		versionDataTxt = (TextView) findViewById(R.id.versionDataTxt);
		
		NetInfo info = getInfo();
		roomNoTxt.setText( roomNoTxt.getText().toString() + ( this.deviceInfo == null?"":this.deviceInfo.getCategoryName() + "-") + ( this.deviceInfo == null?"":this.deviceInfo.getName()));
//		ipTxt.setText(ipTxt.getText().toString() + info.ip);
		baudTxt.setText(baudTxt.getText().toString() + MyApplication.getSerialBaud());
		ipTxt.setText(ipTxt.getText().toString() + app.getMyIpAddr());
		networkMaskTxt.setText(networkMaskTxt.getText().toString() + info.networkMask);
		gatewayTxt.setText(gatewayTxt.getText().toString() + info.gateway);
		dnsTxt.setText(dnsTxt.getText().toString() + info.dns);
		macTxt.setText(macTxt.getText().toString() + HardInfoUtil.getMacAddress());
//		versionTxt.setText(versionTxt.getText().toString() + pkgInfo.versionName + "." +pkgInfo.versionCode);
		if(MyApplication.shareUid == null || !MyApplication.shareUid.equals("android.uid.system")){
			versionTxt.setText(versionTxt.getText().toString() + "v" + MyApplication.getApkVersion() + ".noroot" + "(" + app.getSystemVersion() + ",v" + Constants.LYAGENT_VERSION + ")"); 
		}else{
			versionTxt.setText(versionTxt.getText().toString() + "v" + MyApplication.getApkVersion() + "(" + app.getSystemVersion() + ",v" + Constants.LYAGENT_VERSION + ")"); 
		}
		versionDataTxt.setText(versionDataTxt.getText().toString() + "2015-03-04 18:14");
	}

	private NetInfo getInfo() {
		InputStream is = null;
		List<String> commandResult = new ArrayList<String>();
		Process exec = null;
		try {
			 exec = Runtime.getRuntime().exec("getprop");
			is = exec.getInputStream();
			if (is != null) {
				BufferedReader br = new BufferedReader(new InputStreamReader(is));
				String line = null;
				while((line = br.readLine()) != null) {
					commandResult.add(line);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			exec.destroy();
		}
		
		NetInfo info = new NetInfo();
		info.roomNo = deviceInfo == null?"":deviceInfo.getName();
		for (String s : commandResult) {
			//[dhcp.eth0.ipaddress]: [192.168.2.112]
			if (s.contains("[dhcp.eth0.ipaddress]")) {//ip地址
				info.ip =  s.replace("[dhcp.eth0.ipaddress]", "").replace(": ", "").replace("[", "").replace("]", "");
			} else if (s.contains("[dhcp.eth0.gateway]")) {  //网关 [dhcp.eth0.gateway]: [192.168.2.1]
				info.gateway =  s.replace("[dhcp.eth0.gateway]", "").replace(": ", "").replace("[", "").replace("]", "");
			} else if (s.contains("[dhcp.eth0.mask]")) {  //子网掩码 [dhcp.eth0.mask]: [255.255.255.0]
				info.networkMask =  s.replace("[dhcp.eth0.mask]", "").replace(": ", "").replace("[", "").replace("]", "");
			} else if (s.contains("[dhcp.eth0.dns")) {  //dns [dhcp.eth0.dns1]: [192.168.2.1]
				String dns = s.split(": ")[1].replace("[", "").replace("]", "");
				if (!dns.trim().equals("")) {
					info.dns = dns + "  ";
				}
			}
		}
		if (!info.ip.equals("")) {
			info.mac = getMacAddress(info.ip);
		}
		return info;
	}
	
	private String getMacAddress(String ipaddress) /* throws UnknownHostException */{  
	    String strMacAddr = "";  
	    try {  
	        InetAddress ip = InetAddress.getByName(ipaddress);
	  
	        byte[] b = NetworkInterface.getByInetAddress(ip).getHardwareAddress();  
	        StringBuffer buffer = new StringBuffer();  
	        for (int i = 0; i < b.length; i++) {  
	            if (i != 0) {  
	                buffer.append('-');  
	            }  
	  
	            String str = Integer.toHexString(b[i] & 0xFF);  
	            buffer.append(str.length() == 1 ? 0 + str : str);  
	        }  
	        strMacAddr = buffer.toString().toUpperCase();  
	    } catch (Exception e) {  
	        // TODO Auto-generated catch block  
	        e.printStackTrace();  
	    }  
	  
	    return strMacAddr;  
	}  
	
	static class NetInfo {
		public String roomNo  = "";
		public String ip = "";
		public String networkMask = "";
		public String gateway = "";
		public String dns = "";
		public String mac = "";
	}
}
